package home.dao.impl;

import java.util.List;

import home.dao.CriteriaHome;
import home.dao.DAO;
import home.dao.HomeDAO;
import home.domain.Home;

public class HomeDAOJdbcImpl extends DAO<Home> implements HomeDAO{
	
	@Override
	public List<Home> getForListWithCriteriaHome(CriteriaHome kk) {
		String sql ="SELECT id,name,phone,sex,address FROM book WHERE phone LIKE ?";
		//修改了CriteriaHome的getter方法，使其返回字符串有“%%”。
		//若返回值为null 则返回“%%”，若不为null则返回“%”+value +“%”。
		
		return getForList(sql,kk.getphone());
	}
	@Override
	public List<Home> getAll() {
		String sql ="SELECT id,name,phone,sex,address FROM book";
		return getForList(sql);
	}

	@Override
	public void save(Home home) {
		String sql ="INSERT INTO book(name,phone,sex,address) VALUES(?,?,?,?)";
		update(sql,home.getName(),home.getPhone(),home.getSex(),home.getAddress());
	}

	@Override
	public Home get(Integer id) {
		String sql ="SELECT id,name,phone,sex,address FROM book WHERE id=?";
		return get(sql,id);
	}

	
	@Override
	public void delete(){
		String sql="DELETE FROM book WHERE "
				+ " id NOT IN( SELECT mid FROM("
				+ " SELECT MIN(id) MID FROM book GROUP BY name,phone)AS TT)";
		update(sql);
	}

	
}
